package cn.action.leave;

import cn.entity.ClaimVoucher;
import cn.entity.Employee;
import cn.entity.Leave;
import cn.service.common.EmployeeBiz;
import cn.service.leave.LeaveBiz;
import cn.utils.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class LeaveAction extends ActionSupport {
    private Leave leave;
    private String toMethod;
    private LeaveBiz leaveBiz;
    private EmployeeBiz employeeBiz;
    private Page pageSupport;
    private int pageNo;
    private int pageSize;
    private Date startDate;
    private Date endDate;
    private Map<String,String> leaveTypeMap = new HashMap<String,String>(){{
        put("事假","事假");
        put("婚假","婚假");
        put("年假","年假");
        put("病假","病假");
        put("产假","产假");
        put("陪产假","陪产假");
        put("其他","其他");
    }};

    public String toEdit(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        Employee manager = employeeBiz.findByPidAndDid(2,employee.getSysDepartment().getId());
        session.put("manager",manager);
        toMethod = "/jsp/leave/leave_edit.jsp";
        return SUCCESS;
    }

    public String saveLeave(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        leave.setCreateTime(new Date());
        leave.setCreator(employee);
        leave.setStatus("待审批");
        leaveBiz.saveOrUpdate(leave);
        toMethod="/leave_searchLeave.action";
        return "success-action";
    }

    public String searchLeave(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        pageSupport = (Page) session.get("pageSupport");
        if (pageSupport==null||pageNo==0){
            pageSupport = new Page<Leave>();
            leave = new Leave();
            pageNo=1;
            pageSize=5;
        }
        pageSupport.setCurrPageNo(pageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(leaveBiz.findCountBySn(employee,startDate,endDate));
        pageSupport.setItems(leaveBiz.findBySnAndPage(employee,pageNo,pageSize,startDate,endDate));
        if (pageSupport.getItems().size()<1){
            pageSupport.setCurrPageNo(0);
            pageSupport.setTotalPageCount(0);
            pageSupport.setTotalCount(1111);
        }
        session.put("pageSupport",pageSupport);
        toMethod = "/jsp/leave/leave_list.jsp";
        return SUCCESS;
    }

    public String getLeaveById(){
        leave = leaveBiz.findById(leave.getId());
        toMethod="/jsp/leave/leave_view.jsp";
        return SUCCESS;
    }

    public String toCheck(){
        leave = leaveBiz.findById(leave.getId());
        toMethod="/jsp/leave/leave_check.jsp";
        return SUCCESS;
    }

    public String checkLeave(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        String status = leave.getStatus();
        String approveOpinion = leave.getApproveOpinion();
        leave = leaveBiz.findById(leave.getId());
        leave.setStatus(status);
        leave.setApproveOpinion(approveOpinion);
        leave.setModifyTime(new Date());
        leaveBiz.saveOrUpdate(leave);
        toMethod="/leave_searchLeave.action";
        return "success-action";
    }

    public String getToMethod() {
        return toMethod;
    }

    public Page getPageSupport() {
        return pageSupport;
    }

    public void setPageSupport(Page pageSupport) {
        this.pageSupport = pageSupport;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public void setToMethod(String toMethod) {
        this.toMethod = toMethod;
    }

    public LeaveBiz getLeaveBiz() {
        return leaveBiz;
    }

    public void setLeaveBiz(LeaveBiz leaveBiz) {
        this.leaveBiz = leaveBiz;
    }

    public Leave getLeave() {
        return leave;
    }

    public void setLeave(Leave leave) {
        this.leave = leave;
    }

    public EmployeeBiz getEmployeeBiz() {
        return employeeBiz;
    }

    public void setEmployeeBiz(EmployeeBiz employeeBiz) {
        this.employeeBiz = employeeBiz;
    }

    public Map<String, String> getLeaveTypeMap() {
        return leaveTypeMap;
    }

    public void setLeaveTypeMap(Map<String, String> leaveTypeMap) {
        this.leaveTypeMap = leaveTypeMap;
    }
}
